GetMealTicketsPerMonthQueryHandler.execute   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 51
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 51
c 0
b 0
f 0
rs 7.496
cc 7

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import { QueryHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { IMealTicketRemovalRepository } from 'src/Domain/HumanResource/MealTicket/Repository/IMealTicketRemovalRepository';
4
import { GetMealTicketsPerMonthQuery } from './GetMealTicketsPerMonthQuery';
5
import { IUserRepository } from 'src/Domain/HumanResource/User/Repository/IUserRepository';
6
import {
7
  FindAllEventsByMonth,
8
  IEventRepository
9
} from 'src/Domain/FairCalendar/Repository/IEventRepository';
10
import { ICooperativeRepository } from 'src/Domain/Settings/Repository/ICooperativeRepository';
11
import { CooperativeNotFoundException } from 'src/Domain/Settings/Repository/CooperativeNotFoundException';
12
import { MealTicketsPerMonthView } from '../Views/MealTicketsPerMonthView';
13
import { EventType } from 'src/Domain/FairCalendar/Event.entity';
14
15
@QueryHandler(GetMealTicketsPerMonthQuery)
16
export class GetMealTicketsPerMonthQueryHandler {
17
  constructor(
18
    @Inject('IMealTicketRemovalRepository')
19
    private readonly mealTicketRemovalRepository: IMealTicketRemovalRepository,
20
    @Inject('IUserRepository')
21
    private readonly userRepository: IUserRepository,
22
    @Inject('IEventRepository')
23
    private readonly eventRepository: IEventRepository,
24
    @Inject('ICooperativeRepository')
25
    private readonly cooperativeRepository: ICooperativeRepository
26
  ) {}
27
28
  public async execute(
29
    query: GetMealTicketsPerMonthQuery
30
  ): Promise<MealTicketsPerMonthView[]> {
31
    const cooperative = await this.cooperativeRepository.find();
32
    if (!cooperative) {
33
      throw new CooperativeNotFoundException();
34
    }
35
36
    const { date } = query;
37
    const [users, mealTicketRemovals] = await Promise.all([
38
      this.userRepository.findUsers(false, true),
39
      this.mealTicketRemovalRepository.findByMonth(date)
40
    ]);
41
42
    const events: FindAllEventsByMonth[] = await this.eventRepository.findAllEventsByMonth(
43
      date,
44
      [EventType.OTHER]
45
    );
46
47
    const mealTicketsByUser = [];
48
    const mealTicketsRemovalsByUser = [];
49
    const mealTicketsPerMonthView: MealTicketsPerMonthView[] = [];
50
51
    for (const { duration, user } of events) {
52
      if (duration > cooperative.getDayDuration() / 2) {
53
        mealTicketsByUser[user] = mealTicketsByUser[user] + 1 || 1;
54
      }
55
    }
56
57
    for (const { id, count } of mealTicketRemovals) {
58
      mealTicketsRemovalsByUser[id] = count;
59
    }
60
61
    for (const user of users) {
62
      const mealTicketRemoval = mealTicketsRemovalsByUser[user.getId()] || 0;
63
      const mealTicket =
64
        mealTicketsByUser[user.getId()] - mealTicketRemoval || 0;
65
66
      mealTicketsPerMonthView.push(
67
        new MealTicketsPerMonthView(
68
          user.getId(),
69
          user.getFirstName(),
70
          user.getLastName(),
71
          mealTicket <= 0 ? 0 : mealTicket,
72
          mealTicketRemoval
73
        )
74
      );
75
    }
76
77
    return mealTicketsPerMonthView;
78
  }
79
}
80